<?php
class Faq_model extends MY_Model
{
	public
		$table = 'faq',
		$prKey = 'faq_id',
		$publishedField = 'faq_status',

		$status = array(
			'1' => 'Active',
			'2' => 'Inactive',
		)
	;

	function __construct()
	{
		parent::__construct();

		$this->fields = array(
			'faq_id' 		=> 'ID#',
			'faq_question'	=> T_('Question'),
			'faq_answer'	=> T_('Answer'),
			'faq_created'	=> T_('Date_created'),
			'faq_status' 	=> T_('Status'),
		);
			
		$this->rules = array(
			'faq_id' 		=> "trim|integer",
			'faq_question' 	=> "trim|required|max_length[255]",
			'faq_answer' 	=> "trim|required",
			'faq_created'	=> "trim|max_length[255]",
			'faq_status' 	=> "trim|required|is_natural_no_zero",
		);
	}
	function searchAll() {
		$sql = "SELECT *
				FROM ".$this->table." p
				ORDER by p.faq_created ASC"; 
		$this->data['list'] = $this->db->query($sql)->result();
	
	}
	function search($config = array())
	{
		$config = array_merge(array('rowsOnPage' => 2, 'order' => 'id'), $config);
		//pr($config);
		
		if ($this->input->post('per_page'))
		{
			$config['rowsOnPage'] = $this->input->post('per_page');
			$per_page = 0;
		}		
		else if ($this->input->get('per_page'))
		{
			$per_page = $this->input->get('per_page');
		}
		else
		{
			$per_page = 0;
		}

		// main sql		
		$sql = '';
		$sql .= ' FROM '.$this->table.' p';
		$sql .= ' WHERE 1 = 1 '; 
		
		if (!empty($config['faq_status']))
		{
			$sql .= ' AND p.faq_status=' . $this->db->escape($config['faq_status']); 
		}
		
		switch ($config['order'])
		{
			case 'date':
				$sql .= ' ORDER by p.faq_created';
				break;
			
			default:
				$sql .= ' ORDER by p.faq_id';
		}
		
		$sql .= ' '.$config['direction'];

		$config['total_rows'] = $this->getRowCount($sql);
		
		$this->pagination->initialize($config); 
		
		// main sql
		$sql = 'SELECT *' . $sql . sprintf(' LIMIT %d, %d', $per_page, $config['per_page']); 

		$this->data['list'] = $this->db->query($sql)->result();
		$this->data['per_page'] = $config['per_page'];
	}

	function get($id = null)
	{	
		$this->db->from($this->table);
		$this->db->where($this->table.'.faq_id', $id);
		$query = $this->db->get();

		return $query->row_array();
	}
	
	function delete($items_serialized = null)
	{
		$items = explode("&", $items_serialized);
		
		$error = "";
		$report = "";
		foreach ($items as $id)
		{	
			$data = $this->faq_model->get($id);
			
			$res = $this->db->delete($this->table, array($this->prKey => $id));
			
			if (is_null($res))
			{
				$this->log->writeMessage('error', T_('Error. FAQ Item was not removed.'));
				$report .= ",0"; // don't deleted
			}
			else
			{
				$this->log->writeMessage('done', T_('FAQ Item was removed.'));
				$report .= ",1"; // deleted								
			}							
		}
		
		return substr($report, 1);
	}
		
	function insert($data)
	{
		if (!empty($data))
		{
			$data['faq_created'] = MYSQL_NOW();
			
			parent::insert($data);

			if ($this->db->insert_id() > 0)
			{
				$this->log->writeMessage('done', T_('FAQ Item was added succesfully.'));
				
				return TRUE;
			}
			else
			{
				$this->log->writeMessage('error', T_('Error! FAQ Item was not created.'));
				
				return FALSE;
			}
		}		
	}

	function update($data)
	{
		if (!empty($data))
		{
			$res = parent::update($data);

			$this->log->writeMessage('done', T_('Saved succesfully.'));

			return TRUE;			
		}		
	}		
			
}
?>